home *** CD-ROM | disk | FTP | other *** search
- /* fgets.c - get string from stream.
- K & R page 155.
- Entered - G. R. Mansfield. 84/06/08.
- Ver 1.0-4731.
- */
-
- #include <stdio.h>
-
- char *fgets(s, n, fp) /* get at most n characters from stream */
- char *s;
- int n;
- FILE *fp;
- {
- int c;
- char *cs;
-
- cs = s;
- while (--n > 0 && (c = getc(fp)) != EOF) {
- if ((*cs++ = c) == '\n')
- break;
- }
- if (cs - s >= 2) { /* delete possible CR from end of line */
- if (*(cs-2) == '\r') {
- cs--;
- *(cs-1) = '\n';
- }
- }
- *cs = '\0';
- return((c == EOF && cs == s) ? NULL : s);
- }
-